define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // $('#export').click(function(){
            //     $("#table").tableExport({
            //         type: 'excel',
            //         escape: 'true',
            //         fileName: 'tableExport',
            //         ignoreColumn: [7] //删除第八列
            //     })
            //
            // })
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order/index' + location.search,
                    show_url: 'order/order/show',
                    multi_url: 'order/order/multi',
                    import_url: 'order/order/import',
                    delivery_url: 'order/order/delivery',
                    extended_url: 'order/order/extended',
                    autodelivery_url: 'order/order/autodelivery',
                    table: 'order',
                }
            });

            var table = $("#table");

            table.on('load-success.bs.table', function (e,data) {
                $('#sum').text(data.total);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $('.downid').click(function (){

                var goodsid = $(this).attr('data-value');
                $.ajax({
                    url: 'order/order/orderShut',
                    type: "POST",
                    dataType: "json",
                    data: {id: goodsid},
                    success: function (data){
                        layer.alert(data.msg,{ icon: 1, closeBtn: 0 },function (index){
                            window.location.reload();
                        });
                    },
                    error: function (data){
                        layer.alert(data.msg);
                    }
                })
            });
            $('.tuikuan').click(function (){
                var area = [200,100];
                var goodsid = $(this).attr('data-value');
                Layer.open({
                    content: "确定通过退款吗?",
                    zIndex: 99,
                    area: area,
                    title: "审核退款",
                    resize: false,
                    btn: ["确定","取消"],
                    yes: function (index, layero) {
                        Fast.api.ajax({
                            url: "order/order/refundtui",
                            type: 'post',
                            data: {ids: goodsid}
                        }, function (data, ret) {
                            window.location.reload();
                        }, function (data, ret) {
                        });
                    },
                    btn2: function () {
                        layer.close();
                    },
                    success: function (layero, index) {
                        $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                    },
                    end: function () {
                        $(document).off('keydown', this.checkEnterKey);
                    }
                });
                // $.ajax({
                //     url: 'order/order/orderShut',
                //     type: "POST",
                //     dataType: "json",
                //     data: {id: goodsid},
                //     success: function (data){
                //         layer.alert(data.msg,{ icon: 1, closeBtn: 0 },function (index){
                //             window.location.reload();
                //         });
                //     },
                //     error: function (data){
                //         layer.alert(data.msg);
                //     }
                // })
            });
            $('.jujuetuikuan').click(function (){
                var area = [200,100];
                var goodsid = $(this).attr('data-value');
                Layer.open({
                    content: "确定要拒绝退款吗?",
                    zIndex: 99,
                    area: area,
                    title: "拒绝退款",
                    resize: false,
                    btn: ["确定","取消"],
                    yes: function (index, layero) {
                        Fast.api.ajax({
                            url: "order/order/refundjujue",
                            type: 'post',
                            data: {ids: goodsid}
                        }, function (data, ret) {
                            // layer.alert(data.msg,{ icon: 1, closeBtn: 0 },function (index){
                            //     window.location.reload();
                            // });
                            window.location.reload();
                        }, function (data, ret) {
                        });
                    },
                    btn2: function () {
                        layer.close();
                    },
                    success: function (layero, index) {
                        $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                    },
                    end: function () {
                        $(document).off('keydown', this.checkEnterKey);
                    }
                });
            });
        },

        delivery: function () {
            Controller.api.bindevent();
        },
        show: function () {
            $('.showdelivery').on('click',function (){
                var id = $('input').attr('value');
                var msg = '发货';
                var url = 'order/order/autodelivery?ids=' + id;
                options = {
                    'area':[
                        $(window).width() > '100%' ? '100%' : '100%',
                        $(window).height() > '100%' ? '100%' : '100%'
                    ],
                }
                Fast.api.open(url,msg,options);
            });
            $(".layer-close").click(function () {
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
            })

            Form.api.bindevent($("form[role=form]"),function(data, ret){
                window.location.href = "index";
            });

        },
        extended: function () {
            Controller.api.bindevent();
        },
        userprice: function (){
            Controller.api.bindevent();
        },
        autodelivery: function () {
            Controller.api.bindevent();
            Controller.api.typeRadio();
            Controller.api.editAddress();
            Form.api.bindevent($("form[role=form]"),function(data, ret){
                window.location.href = "index?status=1";
            });

            $(".shaddress").click(function(){
                var name = $(this).attr('data-name');

                if (name === 'goodsaddress')
                {
                    $('.citiesaddress').show();
                    $('.szaddress').hide();
                    $('#native').attr('class',name);
                }

                if (name === 'userupaddress' || name === 'refundaddress')
                {

                    if (name === 'refundaddress')
                    {
                        $('#status').val(2);
                    }
                    if (name === 'userupaddress')
                    {
                        $('#status').val(1);
                    }

                    $('.citiesaddress').hide();
                    $('.szaddress').show();
                }
            });

            $("#upfhaddress").on('click',function (){
                var addressIds = $('.selectpicker').find("option:selected").attr('value');
                var id    = $('#id').text();
                var status = $('#status').val();

                var sh_company  = $('#sh_company').val();
                var sh_name = $('#sh_name').val();
                var sh_phone  = $('#sh_phone').val();
                var sh_native = $('#sh_native').val();
                var sh_address = $('#sh_address').val();

               if (addressIds == 0)
               {
                    if (sh_company === '')
                    {
                        sh_company = status === 1 ? $('#fhdeliv_name').text() :  $('#refunddeliv_company').text();
                    }

                   if (sh_name === '')
                   {
                       sh_name  = status === 1 ? $('#fhdeliv_name').text()  :  $('#refunddeliv_name').text();
                   }

                   if (sh_phone === '')
                   {
                       sh_phone = status === 1 ? $('#fhdeliv_mobile').text()  :  $('#refunddeliv_mobile').text();
                   }

                   if (sh_native === '')
                   {
                       var province = status === 1 ? $('#fhdeliv_province').text() :  $('#refunddeliv_province').text();
                       var city     = status === 1 ? $('#fhdeliv_city').text() : $('#refunddeliv_city').text();
                       var district = status === 1 ? $('#fhdeliv_district').text() : $('#refunddeliv_district').text();
                       sh_native    = province + '/' + city + '/' + district;
                   }

                   if (sh_address === '')
                   {
                       sh_address = status === 1 ? $('#fhdeliv_name').text() : $('#refunddeliv_address').text();
                   }

                   $.getJSON(
                       'order/order/getaddress',
                       {company:sh_company,name:sh_name,phone:sh_phone,native:sh_native,address:sh_address,orderid:id, status:status},
                       function (data) {
                           if (data.code == 1)
                           {
                               if (status == 1)
                               {
                                   $('#fhdeliv_province').text(data.data.province);
                                   $('#fhdeliv_city').text(data.data.city);
                                   $('#fhdeliv_district').text(data.data.area);
                                   $('#fhdeliv_address').text(data.data.address);
                                   $('#fhdeliv_mobile').text(data.data.phone);
                                   $('#fhdeliv_name').text(data.data.name);
                                   $('#fhdeliv_company').text(data.data.company);
                               }else{
                                   $('#fhdeliv_province').text(data.data.province);
                                   $('#refunddeliv_city').text(data.data.city);
                                   $('#refunddeliv_district').text(data.data.area);
                                   $('#refunddeliv_address').text(data.data.address);
                                   $('#refunddeliv_mobile').text(data.data.phone);
                                   $('#refunddeliv_name').text(data.data.name);
                                   $('#refunddeliv_company').text(data.data.company);
                               }
                               layer.alert('添加成功');
                           }else{
                               layer.alert(data.msg)
                           }
                       }
                   )
               }else{
                   $.getJSON(
                       'order/order/getaddress',
                       {ids:addressIds,orderid:id,company:sh_company,name:sh_name,phone:sh_phone,native:sh_native,address:sh_address,status:status},
                       function (data) {
                           if (data.code == 1)
                           {
                               if (status == 1)
                               {
                                   $('#fhdeliv_province').text(data.data.province);
                                   $('#fhdeliv_city').text(data.data.city);
                                   $('#fhdeliv_district').text(data.data.area);
                                   $('#fhdeliv_address').text(data.data.address);
                                   $('#fhdeliv_mobile').text(data.data.phone);
                                   $('#fhdeliv_name').text(data.data.name);
                                   $('#fhdeliv_company').text(data.data.company);
                               }else{
                                   $('#fhdeliv_province').text(data.data.province);
                                   $('#refunddeliv_city').text(data.data.city);
                                   $('#refunddeliv_district').text(data.data.area);
                                   $('#refunddeliv_address').text(data.data.address);
                                   $('#refunddeliv_mobile').text(data.data.phone);
                                   $('#refunddeliv_name').text(data.data.name);
                                   $('#refunddeliv_company').text(data.data.company);
                               }
                               layer.alert('添加成功');
                           }else{
                               layer.alert(data.msg);
                           }
                       }
                   )
               }

            });

            $(".layer-close").click(function () {
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
            })

            $(".deliverytype").on('click', function (){
                var type = $('input:radio:checked').val();
                var ids  = $('#id').text();

                $.getJSON('order/order/deliveltype',{ids:ids,type:type},function (data){
                    if (data.code == 1)
                    {
                        Toastr.success('操作成功');
                    }else{
                        Toastr.error(data.msg);
                    }
                })
            })
        },

        api: {
            editAddress: function(){
                $('#addressId').click(function(){
                    // 获取到
                    var region  = $('#c-native_place').val();
                    var address = $('#edit_address').val();
                    var name  = $('#edit_name').val();
                    var phone = $('#edit_phone').val();
                    var id    = $('#id').text();

                    if (name === '')
                    {
                         name = $('#deliv_name').text();
                    }

                    if (phone === '')
                    {
                         phone = $('#deliv_mobile').text();
                    }

                    if (address === '')
                    {
                         address = $('#deliv_address').text();
                    }

                    if (region === '')
                    {
                        var province = $('#deliv_province').text();
                        var city = $('#deliv_city').text();
                        var district = $('#deliv_district').text();
                         region = province + '/' + city + '/' + district;
                    }

                    if (region === '' || address === '' || id === '')
                    {
                        layer.alert('参数不能为空');
                    }

                    var classname = $('#native').attr('class');
                    if (classname === 'goodsaddress')
                    {
                        $.getJSON('order/order/userAddress', {id: id, region: region, address: address,name:name,phone:phone},
                            function(data){
                                var str = region.split('/');
                                console.log(str);
                                if (str.length >= 2 )
                                {
                                    str[2] = '';
                                }

                                if(data.code == 1)
                                {
                                    $('#deliv_province').text(str[0]);
                                    $('#deliv_city').text(str[1]);
                                    $('#deliv_district').text(str[2]);
                                    $('#deliv_address').text(address);
                                    $('#deliv_name').text(name);
                                    $('#deliv_mobile').text(phone);
                                    layer.alert(data.msg)
                                }else {
                                    layer.alert(data.msg)
                                }
                            });
                    }
                })
            },
            typeRadio: function(){

                // $('#selectAll').on('click',function (){
                //     var textName = $(this).val();
                //     if (textName == '全选')
                //     {
                //         console.log(2)
                //         $("input[name='checkboxsuccess']").prop('checked','true');
                //         $("#selectAll").attr('value','反选');
                //     }else{
                //         console.log(1)
                //         $("#selectAll").attr('value','全选');
                //         $("input[name='checkboxsuccess']").prop('checked',false);
                //     }
                //
                //     console.log(textName + '22222')
                //     // $("#selectAll").attr('id','inverseAll');
                // });
                //
                // $('.selectedsub').on('click',function (){
                //     let idsArray = new Array();
                //     $('input[name="checkboxsuccess"]:checked').each(function (){
                //         console.log($(this).val() + '----------------');
                //         idsArray.push($(this).val());
                //     })
                //     let ids = idsArray.join(',');
                //     $('#id').text(ids);
                //     console.log(ids);
                // });

                $('.delivery').click(function (){
                    // var ids = $('#id').text();
                    // console.log(ids)
                    // return false;
                    var couriername = $('#express').find("option:selected").text();
                    var courierorderno = $('#orderno').val();
                    console.log(courierorderno)
                    var ids = $('#id').text();
                    if (courierorderno == '')
                    {
                        console.log('546*****')
                        return Toastr.error('请填写快递单号');
                    }
                    $.getJSON('order/order/delivery',{ids:ids,delivery_id:courierorderno,delivery_name:couriername},function (data){
                        if (data.code == 1)
                        {
                            return Toastr.success('操作成功');
                        }else{
                            return Toastr.error(data.msg);
                        }
                    });
                });

                $('.automatic_delivery').click(function (){
                    var ids = $('#id').text();

                    $.ajax({
                        url: 'order/order/autodelivery',
                        type: 'post',
                        dataType: 'json',
                        data: {ids:ids},
                        success: function (data){

                            if (data.code == 1)
                            {
                                Toastr.success('发货成功');
                            }else{
                                Toastr.error(data.msg);
                            }

                        },

                    })
                });



                $('#types').click(function (){
                    var type = $("input:radio:checked").val();
                    var orderId = $("#orderId").val();
                    if (type == 2)
                    {
                        $("form[id=cxselectform]").attr('action','http://car.aichetuan.com/CoEHSUIaLu.php/order/order/delivery?ids=' + orderId);
                        $('#typeck').css({display:'none'});
                        $('#typeon').css({display: 'block'});
                    }else{
                        $("form[id=cxselectform]").attr('action','http://car.aichetuan.com/CoEHSUIaLu.php/order/order/autodelivery?ids=' + orderId);
                        $('#typeck').css({display:'block'});
                        $('#typeon').css({display: 'none'});

                    }
                })
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }

        }
    };
    return Controller;
});
